package com.hx.haowumall.haowumallorder.service.impl;

import com.hx.haowumall.haowumallorder.entity.OrderEntity;
import com.hx.haowumall.haowumallorder.entity.OrderReturnReasonEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hx.common.utils.PageUtils;
import com.hx.common.utils.Query;

import com.hx.haowumall.haowumallorder.dao.OrderItemDao;
import com.hx.haowumall.haowumallorder.entity.OrderItemEntity;
import com.hx.haowumall.haowumallorder.service.OrderItemService;

@RabbitListener(queues = {"hello.java.queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 监听队列
     * queues = {"hello.java.queue"} 声明需要监听的队列
     *
     * 参数可以写类型
     * 1、Message message：原生消息头和详细信息 头+体
     * 2、T<发送的消息类型>OrderReturnReasonEntity content
     * 3、Channel channel：当前传输数据的通道
     *
     * Queue：可以很多人都来监听。只要收到消息，队列删除消息，且只有一个可以收到消息
     * 场景：
     *     1)、订单服务启动多个；同一个消息，只能有一个客户端收到
     *     2)、只有一个消息完全处理完，方法运行结束，我们就可以接收到下一个消息
     * */
//    @RabbitListener(queues = {"hello.java.queue"})
    @RabbitHandler
    public void  receiveMessage(Message message, OrderReturnReasonEntity content,
                                Channel channel) throws InterruptedException {

        System.out.println("接收消息内容****："+message+"内容："+content);

//        Thread.sleep(3000);
        System.out.println("消息处理完成=>："+content.getName());

        //channel通道内顺序自增
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            if (deliveryTag %2 == 0){
                //签收消息,非批量确认
                channel.basicAck(deliveryTag,false);
                System.out.println("签收了=>："+deliveryTag);
            }else {
                //退货basicNack(long deliveryTag, boolean multiple, boolean requeue)
                //deliveryTag 当前消息的数字标签
                //multiple 是否批量处理
                //requeue 是否重新入队列
                channel.basicNack(deliveryTag,false,false);
                System.out.println("没有签收=>："+deliveryTag);
            }

        } catch (Exception e) {
            //网络中断
        }
    }

    @RabbitHandler
    public void  receiveMessage2(OrderEntity content) throws InterruptedException {

        System.out.println("接收消息内容："+content);

    }

}